<?php
if(isset($_POST['gpid']))
{
	session_start();
	require_once("../connection.php");
	$gpid = $_POST['gpid'];
	$user = $_SESSION['user'];
	include("../../functions.php");
	include("../../functions_shared.php");
}
else
require_once("php/connection.php");

//name and members
$res = mysql_query("SELECT * FROM site_groups WHERE `id`='$gpid' AND `del`!='1'");
if(!$res) echo mysql_error();

if(mysql_numrows($res) == 0)
{
	echo '<div class="err_box small-font">No such group exists!</div>';
}
else
{
	$gp_name = mysql_result($res,0,'name');
	$gp_visibility = mysql_result($res,0,'visibility');
	$gp_description = mysql_result($res,0,'description');
	
		//checking if the present logged in user is a member or not
	$mem_yon = mysql_numrows(mysql_query("SELECT * FROM site_group_members WHERE `gpid`='$gpid' AND `fbid`='$user' AND `del`!='1'"));
	//checking if the present logged in user is the admin or not
	//$admin_yon = mysql_numrows(mysql_query("SELECT * FROM site_groups WHERE `id`='$gpid' AND `fbid`='$user'"));
	//checking if the present logged in user is the mod or not
	$mod_yon = mysql_numrows(mysql_query("SELECT * FROM site_group_members WHERE `gpid`='$gpid' AND `fbid`='$user' AND `mod`='1' AND `del`!='1'"));

	 
	// the name, number of posts and number of members will always be shown.
	if(!isset($_POST['gpid']))
	{
		echo '<div class="sidebar-box" style="padding:10px;">';
		
		fetch_gp_details($gpid);		
		echo '<br><br>';	
			//menue	
		if($mem_yon!=0)
		{	
		echo '<div class="medium-menu" style="width:125px;float:left">
		<div class="menu-icon"><img src="images/rss.png"></div>
		<div class="menu-text"><span class="group_subscribe ">'.subscribe_status($user, $gpid).'</span></div>';
		echo '<div class="clear"></div></div>';
		
		echo '
		<div class="medium-menu medium-menu-tooltip" name="Manage Circle" style="width:30px;float:left"> 
		<a  href="?cat=group&gpid='.$gpid.'&action=info"><div class="menu-icon"><img src="images/settings.png" /></div></a>
		<!--<div class="menu-text"><a  href="?cat=group&gpid='.$gpid.'&action=info">Manage circle</a></div>-->
		<div class="clear"></div></div>';
		}			
		echo '<div class="clear"></div>';
		
	
		//Submitting a request
		if($mem_yon == 0)
		{
			$res = mysql_query("SELECT * FROM site_requests WHERE `gpid`='$gpid' AND `fbid`='$user'");
			if(!$res) echo mysql_error();
			
			// show the join button in three cases
			// 1. he has never sent a request before
			// 2. he has sent a request before when the circle was not public, but now the circle is not
			//    public now and hence he should see the normal join requests 
			// 3. he had sent a join request earlier and was accepted before but somehow is not a member of 
			//    the circle at present
			if(mysql_numrows($res) == 0 || (mysql_numrows($res) != 0 && $gp_visibility=="public") || mysql_result($res,0,'status') == '1')
			{
				if($gp_visibility == "public")
				{
					?>
					<br><span>You need to join this circle before you can start posting.</span> <br>
					<form onsubmit="return false;"><input type="submit" class="button" value="Join Circle" onclick='add_gp_request(<?php echo $gpid;?>, <?php echo $user;?>, this, "public")'/></form><br>
					<?php
				}
				else
				{
					?>
					<br><span>To join, you need to submit a request to the mods of this circle. </span><br>
					<form onsubmit="return false;"><input type="submit" value="Request" class="button" onclick='add_gp_request(<?php echo $gpid;?>, <?php echo $user;?>, this,"")'/></form><br>
					<?php
				}
			}
			else if( mysql_result($res,0,'status')=='0' && $gp_visibility!="public" )
				echo '<br><span class="err_box">Your request is pending approval!</span><br>';
			
		}
		
		//notification
		// requests only when the visibility is not public
		if($mod_yon == '1' && $gp_visibility!="public")
		{
			$res = mysql_query("SELECT * FROM site_requests WHERE `gpid`='$gpid' AND `status`='0'");
			if(mysql_numrows($res) != 0) 
			echo '<br><strong class="small-font">Add Requests('.mysql_numrows($res).')</strong>
			- <a href="index.php?cat=group&gpid='.$gpid.'&action=info">Manage</a><br>';
			
			disp_user($res,"requests_".$gpid."",$user,"reduced");
		}
	}
	
	//members of the group
	if($gp_visibility != "closed" || $mem_yon!=0)	
	{		
		$res = mysql_query("SELECT * FROM site_group_members WHERE `gpid`='$gpid' AND `del`!='1' ORDER BY `username`");			
		//echo '<br><strong class="small-font">Members ('.mysql_numrows($res).')</strong><br>';			
		if(!isset($_POST['gpid'])) 
		echo '<br><div id="group_'.$gpid.'_friendsList">';		
		else
		echo '<br>';
		disp_user($res,"group_".$gpid."",$user,"group");		
		
		if(!isset($_POST['gpid'])) 
		echo '</div>
		</div>'; // this is for the one with class sidebar box
	}
}  

?>
	